/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim.system;

import camera.AbstractCamera;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Node;
import sim.dynamics.RandomizedModelMotion;
import sim.models.BaseModel;
import com.jme3.renderer.ViewPort;
import java.util.ArrayList;

/**
 *
 * @author Moamen Mokhtar
 */
public class SystemSetup {

    private Node bodies;
    private PhysicsSpace physicsSpace;
    private Node rootNode;
    private ArrayList<AbstractCamera> cameras;
    private int width, height;
    private final Vector3f X = Vector3f.UNIT_X;
    private final Vector3f Y = Vector3f.UNIT_Y;
    private final Vector3f Z = Vector3f.UNIT_Z;

    public SystemSetup(PhysicsSpace physicsSpace, ArrayList<AbstractCamera> cameras, Node rootNode, Node bodies, int w, int h) {
        this.bodies = bodies;
        this.rootNode = rootNode;
        this.physicsSpace = physicsSpace;
        width = w;
        height = h;
        this.cameras = cameras;
        rootNode.attachChild(bodies);
    }

    public void AddDirectLight(ColorRGBA color, Vector3f direction) {
        /* Setup lights */
        DirectionalLight dl1 = new DirectionalLight();
        dl1.setColor(color);
        dl1.setDirection(direction);
        rootNode.addLight(dl1);
    }

    public BaseModel addModel(BaseModel model) {
        model.setMotion(new RandomizedModelMotion(width, height, 3f));
        bodies.attachChild(model.getModel());
        physicsSpace.add(model.getControl());
        return model;
    }

    public void setMasterCamera(Camera cam, ViewPort viewPort, Vector3f location, Vector3f direction, float frustumFar) {
        viewPort.setBackgroundColor(ColorRGBA.Black);
//        cam.setViewPort(viewPortValues[0], viewPortValues[1], viewPortValues[2], viewPortValues[3]);
        cam.setLocation(location);
        cam.lookAt(direction, Z);
        cam.setFrustumFar(frustumFar);
    }

    public void AddCamera(AbstractCamera cam, Vector3f location, Vector3f direction, String name, RenderManager renderManager, boolean isClear, boolean DIVS) {
//        cam.getCam().setViewPort(viewPortValues[0], viewPortValues[1], viewPortValues[2], viewPortValues[3]);
        cam.getCam().setLocation(location);

        cam.getCam().lookAt(direction, Y);
        cameras.add(cam);

        if (DIVS) {
            ViewPort view2 = renderManager.createMainView(name, cam.getCam());
            view2.setClearFlags(true, true, true);
            view2.attachScene(rootNode);
        }
    }
}
